package com.besttone.search.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.besttone.search.model.City;
import com.besttone.search.model.District;
import com.besttone.search.sql.NativeDBHelper;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.util.DisplayMetrics;

public class Constants {
	public static String appKey = "50D033D17E0140F1919EF0508FC9A41E";
	public static String appSecret = "35D31BE854EC4F2BAF9F9A61821C7460";

	public static String apiName = "mobile";
	public static String apiMethod = "getMobileByIMSI";
	// public static String serverAddress =
	// "http://116.228.55.196:8060/open_api/";
	public static String serverAddress = "http://open.118114.cn/api";
	// public static String serverAddress = "http://116.228.55.106/api";
	public static String abServerAddress = "http://openapi.aibang.com/search";
	// public static String abAppKey = "a312b2321506db8a3a566370fdca7e29";
	public static String abAppKey = "f41c8afccc586de03a99c86097e98ccb";

	public static String SPACE = "";

	public static int PAGE_SIZE = 10;
	// 测试地址
	 public static final String serviceURL = "http://116.228.55.13:8085/ch_trs_ws/ch_search";
	 public static String chName= "jichu_client";
	 public static String chKey = "123456";
	//public static final String serviceURL = "http://116.228.55.103:9273/ch_trs_ws/ch_search";
//	public static String chName = "jichu_client";
//	public static String chKey = "j0c7c2t9";
	 
	 //关键词联想
	 public static String associate_method = "searchCHkeyNum";

	public static final String NATIVE_DATABASE_NAME = "native_database.db";
	public static int mDeviceHeight;
	public static int mDeviceWidth = 0;

	static {
		mDeviceHeight = 0;
	}

	public static void copyNativeDB(Context paramContext,
			InputStream paramInputStream) {
		try {
			File localFile1 = new File("/data/data/"
					+ paramContext.getPackageName() + "/databases/");
			if (!localFile1.exists())
				localFile1.mkdir();
			String str = "/data/data/" + paramContext.getPackageName()
					+ "/databases/" + "native_database.db";
			File localFile2 = new File(str);
			if (localFile2.exists())
				localFile2.delete();
			FileOutputStream localFileOutputStream = new FileOutputStream(str);
			byte[] arrayOfByte = new byte[18000];
			while (true) {
				int i = paramInputStream.read(arrayOfByte);
				if (i <= 0) {
					localFileOutputStream.close();
					paramInputStream.close();
					break;
				}
				localFileOutputStream.write(arrayOfByte, 0, i);
			}
		} catch (Exception localException) {
		}
	}

	public static City getCity(Context paramContext, String paramString) {
		City localCity = null;
		if ((paramString != null) && (!paramString.equals(""))) {
			NativeDBHelper localNativeDBHelper = NativeDBHelper
					.getInstance(paramContext);
			String[] arrayOfString = new String[2];
			arrayOfString[0] = "2";
			arrayOfString[1] = paramString.trim();
			Cursor localCursor = localNativeDBHelper.select(null,
					"TYPE=? AND REGION_CODE=?", arrayOfString);
			if ((localCursor != null) && (localCursor.getCount() > 0)
					&& (localCursor.moveToFirst())) {
				localCity = new City();
				localCity.setAreaCode(localCursor.getString(9));
				localCity.setCityCode(localCursor.getString(1));
				localCity.setCityId(localCursor.getString(0));
				localCity.setCityName(localCursor.getString(5));
				localCity.setProvinceCode(localCursor.getString(2));
			}
			if (localCursor != null)
				localCursor.close();
			if (localNativeDBHelper != null)
				localNativeDBHelper.close();
		}
		return localCity;
	}

	public static String getCityCode(Context paramContext, String paramString) {
		String str = "";
		if ((paramString != null) && (!paramString.equals(""))) {
			NativeDBHelper localNativeDBHelper = NativeDBHelper
					.getInstance(paramContext);
			if (!paramString.startsWith("0"))
				paramString = "0" + paramString;
			String[] arrayOfString1 = new String[4];
			arrayOfString1[0] = "ID";
			arrayOfString1[1] = "REGION_CODE";
			arrayOfString1[2] = "NAME";
			arrayOfString1[3] = "AREA_CODE";
			String[] arrayOfString2 = new String[2];
			arrayOfString2[0] = "2";
			arrayOfString2[1] = paramString;
			Cursor localCursor = localNativeDBHelper.select(arrayOfString1,
					"TYPE=? AND AREA_CODE=?", arrayOfString2);
			if ((localCursor != null) && (localCursor.getCount() > 0)
					&& (localCursor.moveToFirst()))
				str = localCursor.getString(1);
			if (localCursor != null)
				localCursor.close();
			if (localNativeDBHelper != null)
				localNativeDBHelper.close();
		}
		return str;
	}

	public static String getCodeByName(Context paramContext, String paramString) {
		String str = null;
		NativeDBHelper localNativeDBHelper = NativeDBHelper
				.getInstance(paramContext);
		Cursor localCursor = localNativeDBHelper.selectCodeByName(paramString);
		if ((localCursor != null) && (localCursor.getCount() > 0))
			localCursor.moveToFirst();
		while (true) {
			if (localCursor.isAfterLast()) {
				if (localCursor != null)
					localCursor.close();
				if (localNativeDBHelper != null)
					localNativeDBHelper.close();
			}
			str = localCursor.getString(1);
			localCursor.moveToNext();
			return str;
		}
		
	}
	
	public static String getSimplifyCodeByName(Context paramContext, String paramString) {
		String cityCode = getCodeByName(paramContext, paramString);
		return getSimplifyCode(cityCode);
	}

	public static int getDeviceWidth(Activity paramActivity) {
		if (mDeviceWidth == 0) {
			DisplayMetrics localDisplayMetrics = new DisplayMetrics();
			paramActivity.getWindowManager().getDefaultDisplay()
					.getMetrics(localDisplayMetrics);
			mDeviceWidth = localDisplayMetrics.widthPixels;
		}
		return mDeviceWidth;
	}
	

	  public static String[] getDistrictArray(Context paramContext, String paramString)
	  {
	    String[] arrayOfString = null;
	    List localList = getDistrictList(paramContext, paramString);
	    if ((localList != null) && (localList.size() > 0))
	    {
	      arrayOfString = new String[1 + localList.size()];
	      arrayOfString[0] = "全部区域";
	    }
	    for (int i = 0; ; i++)
	    {
	      if (i >= localList.size())
	        return arrayOfString;
	      arrayOfString[(i + 1)] = ((District)localList.get(i)).getDistrictName();
	    }
	  }

	  public static List<District> getDistrictList(Context paramContext, String paramString)
	  {
	    ArrayList localArrayList = null;
	    NativeDBHelper localNativeDBHelper = NativeDBHelper.getInstance(paramContext);
	    paramString = paramString.substring(0,2) + "%";
	    Cursor localCursor = localNativeDBHelper.selectDistrict(paramString);
	    if ((localCursor != null) && (localCursor.getCount() > 0))
	    {
	      localArrayList = new ArrayList();
	      localCursor.moveToFirst();
	    }
	    while (true)
	    {
	      if (localCursor.isAfterLast())
	      {
	        if (localCursor != null)
	          localCursor.close();
	        if (localNativeDBHelper != null)
	          localNativeDBHelper.close();
	        return localArrayList;
	      }
			District localDistrict = new District();
			localDistrict.setDistrictId(localCursor.getString(0));
			localDistrict.setDistrictName(localCursor.getString(4));
			localDistrict.setDistrictCode(localCursor.getString(1));
			localDistrict.setCityCode(localCursor.getString(2));
			String simplifyCode = localCursor.getString(2);
			if (simplifyCode != null && simplifyCode.endsWith("00")) {
				simplifyCode = simplifyCode.substring(0,
						simplifyCode.length() - 2);
			}
			if (simplifyCode != null && simplifyCode.endsWith("00")) {
				simplifyCode = simplifyCode.substring(0,
						simplifyCode.length() - 2);
			}
			localDistrict.setSimplifyCode(simplifyCode);
			localArrayList.add(localDistrict);
			localCursor.moveToNext();
	    }
	  }

	public static JSONObject getJSONObject(HashMap<String, String> paramHashMap) {
		JSONObject localJSONObject = new JSONObject();
		Iterator localIterator = paramHashMap.entrySet().iterator();
		while (true) {
			if (!localIterator.hasNext())
				return localJSONObject;
			Map.Entry localEntry = (Map.Entry) localIterator.next();
			try {
				localJSONObject.put((String) localEntry.getKey(),
						localEntry.getValue());
			} catch (JSONException localJSONException) {
				localJSONException.printStackTrace();
			}
		}
	}



	public static String getNameByCode(Context paramContext, String paramString) {
		String str = null;
		NativeDBHelper localNativeDBHelper = NativeDBHelper
				.getInstance(paramContext);
		Cursor localCursor = localNativeDBHelper.selectNameByCode(paramString);
		if ((localCursor != null) && (localCursor.getCount() > 0))
			localCursor.moveToFirst();
		while (true) {
			if (localCursor.isAfterLast()) {
				if (localCursor != null)
					localCursor.close();
				if (localNativeDBHelper != null)
					localNativeDBHelper.close();
				return str;
			}
			str = localCursor.getString(4);
			localCursor.moveToNext();
		}
	}

	public static String getProvinceId(Context paramContext, String paramString) {
		String str = "";
		if (!StringUtils.isEmpty(paramString)) {
			NativeDBHelper localNativeDBHelper = NativeDBHelper
					.getInstance(paramContext);
			String[] arrayOfString1 = new String[1];
			arrayOfString1[0] = "ID";
			String[] arrayOfString2 = new String[2];
			arrayOfString2[0] = "1";
			arrayOfString2[1] = paramString;
			Cursor localCursor = localNativeDBHelper.select(arrayOfString1,
					"TYPE=? AND REGION_CODE=?", arrayOfString2);
			if ((localCursor != null) && (localCursor.getCount() > 0)
					&& (localCursor.moveToFirst()))
				str = localCursor.getString(0);
			if (localCursor != null)
				localCursor.close();
			if (localNativeDBHelper != null)
				localNativeDBHelper.close();
		}
		return str;
	}

	public static boolean isBusinessOpen(Context paramContext,
			String paramString) {
		boolean flag = false;
		if ((paramString != null) && (!paramString.equals(""))) {
			NativeDBHelper localNativeDBHelper = NativeDBHelper
					.getInstance(paramContext);
			String[] arrayOfString1 = new String[1];
			arrayOfString1[0] = "BUSINESS_FLAG";
			String[] arrayOfString2 = new String[1];
			arrayOfString2[0] = paramString;
			Cursor localCursor = localNativeDBHelper.select(arrayOfString1,
					"REGION_CODE=?", arrayOfString2);
			if ((localCursor != null) && (localCursor.getCount() > 0)
					&& (localCursor.moveToFirst())) {
				String str = localCursor.getString(0);
				if ((str != null) && (str.equals("1")))
					flag = true;
			}
			if (localCursor != null)
				localCursor.close();
			if (localNativeDBHelper != null)
				localNativeDBHelper.close();
		}
		return flag;
	}
	
	public static String getSimplifyCode(String cityCode) {
		if(cityCode!=null) {
			if(cityCode.endsWith("00")) {
				cityCode = cityCode.substring(0, cityCode.length()-2);
			}
			if(cityCode.endsWith("00")) {
				cityCode = cityCode.substring(0, cityCode.length()-2);
			}
		}
		return cityCode;
	}
	
	public static boolean isAlphabet(String s) {
		if(s!=null) {
			return s.matches("^[a-zA-Z]*$");
		}
		return false;
	}
	
	public static String getCity(String s){
		String city = null;
		if(s!=null && s.trim().length()!=0) {
			String[] cityArray = s.split("\\|\\|");
			int index = cityArray.length - 1;
			city = cityArray[index];
		}
		return city;
	}

}
